<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\User;
use app\admin\service\LoginService;
use think\Db;
use think\Request;

/**
 * 系统登录-控制器
 * @author 牧羊人
 * @since 2020/11/19
 * Class Login
 * @package app\admin\controller
 */
class Login extends Backend
{
    /**
     * 初始化
     * @author 牧羊人
     * @since 2020/11/19
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new LoginService();
    }

    public function test3()
    {
        //["https://edutiyu.oss-cn-shanghai.aliyuncs.com/image/676936e027271.jpg"]
        Db::table('think_equip')->where('id > 21')->update(['pic_data' => '["https://edutiyu.oss-cn-shanghai.aliyuncs.com/image/676936e027271.jpg"]']);
        die();
        $file = fopen('3.csv', 'r');
        $arr = [];
        $i = 0;
        while (($line = fgetcsv($file))!== false) {
            //print_r($line);
            $arr[$i]['no']= trim($line[0]);
            $arr[$i]['room']= trim($line[1],',');
            $i++;
        }
        fclose($file);
       // print_r($arr);die();
        $u = 0;
        $i = 0;
        foreach($arr as $v){
            $pass = 'Aa2024';
            $password1 = get_password($pass .$v['no']);
            $row = Db::table('think_equip')->where('account',$v['no'])->find();
            if($row){
                Db::table('think_equip')->where('id',$row['id'])->update(['password' => $password1]);
                Db::table('think_user')->where('id',$row['admin_id'])->update([
                    'password' => $password1,
                    'username' => $v['no']
                ]);
                $u++;
            }else{
                $i++;
                $insert = [
                    'school_id' => 11,
                    'user_type' => 2,
                    'realname' => $v['no'].$v['room'],
                    'nickname' => $v['no'].$v['room'],
                    'create_time' => time(),
                    'username' => $v['no'],
                    'password' => $password1,
                ];
                $admin_id = Db::table('think_user')->insertGetId($insert);
                $e = [
                    'organization' => 11,
                    //'grade' => $v['grade'],
                    //'class_id' => $v['id'],
                    'scene_id' => '6',
                    'name' => $v['no'].$v['room'],
                    'code' => $v['no'],
                    'account' => $v['no'],
                    'password' => $password1,
                    'admin_id' => $admin_id,
                    'kouhao' => '笃行不倦 止于至善',
                    'target' => '笃行不倦 止于至善',
                    'geyan' => '笃行不倦 止于至善',
                    'pic_data' => '["https://edutiyu.oss-cn-shanghai.aliyuncs.com/image/67693bfac30fc.png"]',
                ];
                Db::table('think_equip')->insertGetId($e);
            }
        }
        echo $i.PHP_EOL.'-U'.$u;
    }

    public function stulogin()
    {

        $input = input();
        //老师登录
        if(isset($input['roletype']) && $input['roletype'] == 2){
            if(empty($input['realname']) || empty($input['roll'])){
                $result = message("请输入手机号或姓名", false);
                return $this->jsonReturn($result);
            }
            $map = [
                'realname' => $input['realname'],
                'mobile' => $input['roll'],
                'mark' => 1,
            ];
            $row = Db::table('think_user')->where($map)->find();
            if(empty($row)){
                $result = message("手机号或姓名错误", false);
                return $this->jsonReturn($result);
            }
        }else{
            //学生登录
            if(empty($input['realname']) || empty($input['roll'])){
                $result = message("请输入姓名和学籍号", false);
                return $this->jsonReturn($result);
            }
            $map = [
                'name' => $input['realname'],
                'id_card' => $input['roll'],
                'mark' => 1,
            ];
            $row = Db::table('think_student')->where($map)->find();
            if(empty($row)){
                $result = message("姓名和学籍号错误", false);
                return $this->jsonReturn($result);
            }
        }

        //realname
        //
        // JWT生成token
        $jwt = new \Jwt();
        $token = $jwt->getToken($row['id']);
        // 结果返回
        $result = [
            'access_token' => $token,
        ];
        $result = message('登录成功', true, $result);
        return $this->jsonReturn($result);
    }

    public function loginByAvatar(Request $request)
    {
        $avatar = (new Upload)->aliUploadImage($request, true);
        try {
            $studentId = \AliFaceBodyService::searchFace($avatar);
        } catch (\Exception $exception) {
            return $this->jsonReturn(message($exception->getMessage(), false));
        }
        $jwt   = new \Jwt();
        $token = $jwt->getToken($studentId);

        return $this->jsonReturn(message('登录成功', true, ['access_token' => $token]));
    }

    public function elecard()
    {
        $result = $this->service->login2();
        return $this->jsonReturn($result);
    }

    /**
     * 退出系统
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function  elecardout()
    {
        $param = request()->param();
        // 登录账号
        $username = trim($param['username']);
        $res = '';
        if (!$username) {
            $res= message('登录账号不能为空', false);
            return $this->jsonReturn($res);
        }
        // 登录密码
        $password = trim($param['password']);
        if (!$password) {
            $res = message('登录密码不能为空', false);
            return $this->jsonReturn($res);
        }
        $this->model = new User();
        // 用户验证
        $info = $this->model->getOne([
            ['username', '=', $username],
        ]);
        if (!$info) {
            $res = message('登录账号不存在', false);
            return $this->jsonReturn($res);
        }
        // 密码校验
        $password = get_password($password . $username);
        if ($password != $info['password']) {
            $res = message("登录密码不正确", false);
            return $this->jsonReturn($res);
        }
        $result = $this->service->logout();
        return $this->jsonReturn($result);
    }
    /**
     * 验证码
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function captcha()
    {
        $result = $this->service->captcha();
        return $this->jsonReturn($result);
    }

    /**
     * 登录
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function login()
    {
        $result = $this->service->login();
        return $this->jsonReturn($result);
    }

    /**
     * 退出系统
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function logout()
    {
        $result = $this->service->logout();
        return $this->jsonReturn($result);
    }

}